﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.Permission;
using Enterprise.Data.Permission;
namespace Enterprise.Service.Permission
{
    /// <summary>
    /// 权限按钮应用服务
    /// </summary>
    public class PermissionButtonService
    {

        /// <summary>
        ///  权限值缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-PermissionButton";

        /// <summary>
        /// 权限按钮份组列表 返回所有权限值
        /// </summary>
        /// <returns></returns>
        public List<PermissionButtonEntity> PermissionButtonList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<PermissionButtonEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                PermissionButtonData data = new PermissionButtonData();
                List<PermissionButtonEntity> list = data.PermissionButtonList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 权限按钮分组 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void PermissionButtonExecute(PermissionButtonEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_PERMISSIONBUTTON(PACTION,PBNAME,PBGROUP,PORDER,PCOMMENT) values('" + entity.PACTION + "','" + entity.PBNAME + "','" + entity.PBGROUP + "'," + entity.PORDER + ",'" + entity.PCOMMENT + "')";
                    break;
                case "Update":
                    //cmd = "Update SYS_PERMISSIONBUTTON set PBGROUP=" + entity.PKEY + " and PNAME='" + entity.PNAME + "' where PACTION=''" + entity.PACTION+"'';
                    break;
                case "Delete":
                    cmd = string.Format("Delete from SYS_PERMISSIONBUTTON where PACTION='{0}' and PBNAME='{1}'", entity.PACTION, entity.PBNAME);
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                PermissionButtonExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 组织机构操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void PermissionButtonExecuteBySql(string cmd)
        {
            PermissionButtonData data = new PermissionButtonData();
            data.PermissionButtonExecute(cmd);
        }

    }
}
